#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

import linecache


def method4(path):
    lines = linecache.getlines(path)
    lss = []
    for line in lines:
        ls = line.strip()
        if "filename" in ls and '.ass' in ls:
            # if ".tx\"" not in line:
            path = ls.replace('\'', '').replace('filename ', '')
            if not os.path.exists(path):
                lss.append(path)
    # 清除缓存
    linecache.clearcache()
    # 75 s
    return lss

def check_filename(data):
    problem_line = []
    line_count = 0
    last_line = ""
    for line in data:
        line_count += 1
        if line.startswith(" filename"):

            if ".tx\"" not in line:
                problem_line.append(line_count)
                print(last_line.replace(" name ", ""))
                print(line.replace(" filename ", ""))
        last_line = line
    print(problem_line)
    return problem_line


class CheckAssInAss(CheckBase):
    def __init__(self):
        super(CheckAssInAss, self).__init__()
        pass

    def get_dso_list(self):
        ass_list = cmds.ls(type='aiStandIn')
        dso_list = []
        for ass in ass_list:
            dso_path = cmds.getAttr(ass + '.dso')
            if dso_path not in dso_list:
                dso_list.append(dso_path)
        return dso_list

    def check(self):
        error_list = []
        dso_list = self.get_dso_list()

        for dso in dso_list:
            pass
            in_file_list = method4(dso)
            # log.info(dso)
            if in_file_list:
                _data = {dso: in_file_list}
                error_list.append(dso)
            # error_list += in_file_list
        # print error_list
        if error_list:
            result_log = u'Error:%s\n' % error_list
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log